<template>
  <div class="searchBox">
    <input
      class="searchInput"
      type="text"
      name=""
      placeholder="描述你的知识点"
      :disabled="loading"
      v-model="inputValue"
    />
    <button class="searchButton" :disabled="loading" @click="handleClick">
      <template v-if="loading">
        <!-- 显示加载中的Loading -->
        <div class="momentum"></div>
      </template>
      <template v-else>
        <!-- 显示默认的搜索图标 -->
        <img
          src="../../assets/img/generate.png"
          alt="search icon"
          width="30"
          height="30"
        />
      </template>
    </button>
  </div>
</template>

<script>
// import VerseLoading from "../UIVerse/VerseLoading.vue";
export default {
  data() {
    return {
      loading: false, // 数据属性，用于跟踪按钮是否在加载中
      inputValue: "",
    };
  },
  methods: {
    handleClick() {
      this.$emit("click");
      this.loading = true; // 设置为加载状态，按钮被禁用
    },
  },
  watch: {
    inputValue(newVal) {
      // 当输入框的值发生变化时触发事件
      this.$emit("input-change", newVal); // 向父组件发送输入值
    },
  },
  created() {
    this.$root.$on("close-animation", () => {
      this.loading = false; // 停止加载状态
    });
  },
};
</script>

<style lang="less">
.searchBox {
  display: flex;
  min-width: 800px;
  max-width: 800px;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: #242431;
  border-radius: 50px;
  position: relative;
}

.searchButton {
  color: white;
  position: absolute;
  right: 8px;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: var(
    --gradient-2,
    linear-gradient(90deg, #2af598 0%, #009efd 100%)
  );
  border: 0;
  display: inline-block;
  transition: all 300ms cubic-bezier(0.23, 1, 0.32, 1);
  cursor: pointer; /* 设置为可点击的样式 */
  &:disabled {
    opacity: 0.5; /* 设置禁用时的外观 */
    cursor: not-allowed; /* 禁用时鼠标样式 */
  }
}
/* 按钮悬停效果 */
.searchButton:hover:not(:disabled) {
  color: #fff;
  background-color: #1a1a1a;
  box-shadow: rgba(0, 0, 0, 0.5) 0 10px 20px;
  transform: translateY(-3px);
}
/* 按钮按下效果 */
.searchButton:active:not(:disabled) {
  box-shadow: none;
  transform: translateY(0);
}

.searchInput {
  border: none;
  background: none;
  outline: none;
  color: white;
  font-size: 20px; /* 增加字体大小 */
  padding: 24px 46px 24px 26px;
  width: 100%;
}

.loadingIcon {
  width: 10px;
  height: 10px;
}

// UIVerse的一个loading效果
.momentum {
  --uib-size: 40px;
  --uib-speed: 1s;
  --uib-color: rgb(221, 255, 242);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  height: var(--uib-size);
  width: var(--uib-size);
  animation: rotate01561 var(--uib-speed) linear infinite;
}

.momentum::before,
.momentum::after {
  content: "";
  height: 25%;
  width: 25%;
  border-radius: 50%;
  background-color: var(--uib-color);
}

.momentum::before {
  animation: wobble290123 calc(var(--uib-speed) * 1.25) ease-in-out infinite;
}

.momentum::after {
  animation: wobble9123 calc(var(--uib-speed) * 1.25) ease-in-out infinite;
}

.momentum::before {
  margin-right: 10%;
}

@keyframes wobble9123 {
  0%,
  100% {
    transform: translateX(0);
  }

  50% {
    transform: translateX(calc(var(--uib-size) * 0.2)) scale(1.1);
  }
}

@keyframes wobble290123 {
  0%,
  100% {
    transform: translateX(0);
  }

  50% {
    transform: translateX(calc(var(--uib-size) * -0.2)) scale(1.1);
  }
}

@keyframes rotate01561 {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}
</style>
